package com.carefree.bookmall.service.impl;

import com.carefree.bookmall.common.utils.Constant;
import com.carefree.bookmall.common.vo.BookQueryVo;
import com.carefree.bookmall.common.vo.PageVO;
import com.carefree.bookmall.dao.BookMapper;
import com.carefree.bookmall.dao.RecommendMapper;
import com.carefree.bookmall.entity.Book;
import com.carefree.bookmall.exception.CustomException;
import com.carefree.bookmall.service.BookService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName:Book
 * @Description:
 * @Author Tengbin
 */
@Service(value = "bookService")
public class BookServiceImpl implements BookService {

    @Resource
    private BookMapper bookMapper;

    @Resource
    private RecommendMapper recommendMapper;


    /**
     * 根据类型查询书籍信息
     *
     * @param recommendType 推荐类型
     * @param pageNumber    页数
     * @param pageSize      每页显示数
     * @return
     */
    @Override
    public List<BookQueryVo> queryBookByRecommendType(Integer recommendType, int pageNumber, int pageSize) throws CustomException {

        List<BookQueryVo> books = recommendMapper.selectBookByRecommendType(recommendType, (pageNumber - 1) * pageSize, pageSize);

        if (books.isEmpty()) {
            throw new CustomException("没有查询到该推荐类型的书籍信息，请检查后重试！");
        }

        return books;
    }

    /**
     * 根据类型分页查询书籍信息
     *
     * @param recommendType 推荐类型
     * @param pageNumber    页数
     * @param pageSize      每页显示数
     * @return
     */
    @Override
    public PageVO pageBookByRecommendType(String recommendType, int pageNumber, int pageSize) throws CustomException {

        int recommendedTypeNum = getRecommendedTypeNum(recommendType);

        if (pageNumber <= 0) {
            pageNumber = 1;
        }
        // 实例化自定义分页对象并赋值
        PageVO pageVO = new PageVO();
        List<BookQueryVo> books = Collections.emptyList();
        pageVO.setPageNumber(pageNumber);

        int totalCount = 0;

        if (recommendedTypeNum == 0) {
            totalCount = bookMapper.selectCountOfBooks();
            books = bookMapper.selectBooks((pageNumber - 1) * pageSize, pageSize);
        } else {
            totalCount = recommendMapper.selectRecommendCountOfBooksByRecommendType(recommendedTypeNum);
            books = recommendMapper.selectBookByRecommendType(recommendedTypeNum, (pageNumber - 1) * pageSize, pageSize);
        }

        // 自定义属性赋值
        for (BookQueryVo bookQueryVo : books) {
            bookQueryVo.setIsscroll(recommendMapper.selectBookByRecommendTypeAndBookId(1, bookQueryVo.getId()) >= 1);
            bookQueryVo.setIshot(recommendMapper.selectBookByRecommendTypeAndBookId(2, bookQueryVo.getId()) >= 1);
            bookQueryVo.setIsnew(recommendMapper.selectBookByRecommendTypeAndBookId(3, bookQueryVo.getId()) >= 1);
        }

        // page对象赋值
        pageVO.SetPageSizeAndTotalCount(pageSize, totalCount);
        pageVO.setList((List) books);

        return pageVO;
    }


    /**
     * 估计书籍id查询书籍信息
     *
     * @param bookId 书籍id
     * @return Book
     */
    @Override
    public Book queryBookById(Integer bookId) throws CustomException {
        Book book = bookMapper.selectBookByBookId(bookId);

        if (ObjectUtils.isEmpty(book)) {
            throw new CustomException("没查到你要查的书籍信息，请检查后重试");
        }

        return book;
    }

    /**
     * 估计书籍id查询书籍信息
     *
     * @param bookId 书籍id
     * @return BookQueryVo
     */
    @Override
    public BookQueryVo queryBookByBookId(Integer bookId) throws CustomException {
        if (bookId == null) {
            throw new CustomException("未获取到该书籍的查询必要信息！");
        }
        BookQueryVo bookQueryVo = bookMapper.selectBookById(bookId);

        if (ObjectUtils.isEmpty(bookQueryVo)) {
            throw new CustomException("未查询到该书籍信息！请检查后重试");
        }

        return bookQueryVo;
    }

    /**
     * 根据关键字分页查询书籍信息
     *
     * @param keyword    搜索关键字
     * @param pageNumber 页数
     * @return
     */
    @Override
    public PageVO searchBooksByKeyword(String keyword, int pageNumber, int pageSize) {
        if (pageNumber <= 0) {
            pageNumber = 1;
        }
        int totalCount = 0;
        List<BookQueryVo> books = Collections.emptyList();
        // 实例化自定义page对象并赋值最后返回
        PageVO pageVO = new PageVO();
        pageVO.setPageNumber(pageNumber);

        // 根据关键字查询总数
        // 查询符合关键字信息的书籍集合信息
        if (StringUtils.isBlank(keyword)) {
            totalCount = bookMapper.selectCountOfBooks();
            books = bookMapper.selectBooks((pageNumber - 1) * pageSize, pageSize);
        } else {
            totalCount = bookMapper.selectCountOfBooksByKeyword(keyword);
            books = bookMapper.selectBooksByKeyword(keyword, (pageNumber - 1) * pageSize, pageSize);
        }

        pageVO.SetPageSizeAndTotalCount(pageSize, totalCount);
        pageVO.setList((List) books);

        return pageVO;
    }

    /**
     * 添加推荐类型
     *
     * @param bookId        书籍id
     * @param recommendType 推荐类型
     * @return
     */
    @Override
    public boolean addRecommend(Integer bookId, Integer recommendType) throws CustomException {
        if (bookId == null || recommendType == null) {
            throw new CustomException("添加推荐类型信息的必要参数不能为空！");
        }

        return recommendMapper.addRecommendBook(recommendType, bookId) > 0;
    }

    /**
     * 删除推荐类型信息
     *
     * @param bookId
     * @param recommendType
     * @return
     */
    @Override
    public boolean remoteRecommend(Integer bookId, Integer recommendType) throws CustomException {
        if (bookId == null || recommendType == null) {
            throw new CustomException("删除推荐类型信息的必要参数不能为空！");
        }
        return recommendMapper.removeRecommendBook(recommendType, bookId) > 0;
    }

    /**
     * 根据书籍id删除书籍信息
     *
     * @param bookId 书籍id
     * @return
     */
    @Override
    public boolean removeBookByBookId(Integer bookId) throws CustomException {
        if (bookId == null) {
            throw new CustomException("需删除的书籍id必须不为空！");
        }

        return bookMapper.removeBook(bookId) > 0;
    }

    /**
     * 添加书籍信息
     *
     * @param book 书籍信息
     * @return
     */
    @Override
    public boolean addBook(Book book) throws CustomException {
        if (ObjectUtils.isEmpty(book)) {
            throw new CustomException("需添加的商品必须不为空！");
        }
        return bookMapper.addBook(book) > 0;
    }

    /**
     * 修改书籍信息
     *
     * @param book 书籍信息
     * @return
     */
    @Override
    public boolean modifyBook(Book book) {
        return bookMapper.modifyBook(book) > 0;
    }


    /**
     * 根据推荐类型返回对应的查询参数
     *
     * @param recommendType 推荐类型（all、scroll、hot、new）
     * @return int整数
     * @throws CustomException
     */
    private int getRecommendedTypeNum(String recommendType) throws CustomException {
        int recommendedTypeNum = - 1;

        if (Constant.BOOK_ALL.equals(recommendType)) {
            recommendedTypeNum = 0;
        } else if (Constant.BOOK_SCROLL.equals(recommendType)) {
            recommendedTypeNum = 1;
        } else if (Constant.BOOK_HOT.equals(recommendType)) {
            recommendedTypeNum = 2;
        } else if (Constant.BOOK_NEW.equals(recommendType)) {
            recommendedTypeNum = 3;
        } else {
            throw new CustomException("非正常访问路径，请检查后重试");
        }
        return recommendedTypeNum;
    }
}
